package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportUserRpcService;
import com.hunttown.mes.rpc.domain.TransportUserDTO;
import com.hunttown.mes.rpc.domain.query.TransportUserDTOQuery;
import com.hunttown.mes.service.TransportUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.hunttown.mes.convert.TransportUserConverts.convert;

/**
 * created by wangjunfu on 2019-05-18 20:14:43
 */
@Service
public class TransportUserRpcServiceImpl implements TransportUserRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportUserRpcServiceImpl.class);

    private final TransportUserService transportUserService;

    @Autowired
    public TransportUserRpcServiceImpl(TransportUserService transportUserService) {
        this.transportUserService = transportUserService;
    }

    @Override
    public TransportUserDTO insert(TransportUserDTO transportUserDTO) {
        try {
            return convert(transportUserService.insert(convert(transportUserDTO)));
        } catch (Exception e) {
            logger.error("TransportUser create failed.", e);
            throw new RuntimeException("TransportUser create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportUserDTO transportUserDTO) {
        try {
            return transportUserService.updateInfoById(convert(transportUserDTO));
        } catch (Exception e) {
            logger.error("TransportUser update by id failed.", e);
            throw new RuntimeException("TransportUser update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportUserDTOQuery transportUserDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportUserDTOQuery.getW_id() != null && transportUserDTOQuery.getW_id() > 0) {
                query.put("w_id", transportUserDTOQuery.getW_id());
            }

            if (StringUtils.isNotBlank(transportUserDTOQuery.getW_mobile())) {
                query.put("w_mobile", transportUserDTOQuery.getW_mobile());
            }

            if (transportUserDTOQuery.getIds() != null && !transportUserDTOQuery.getIds().equals("")) {
                query.put("ids", transportUserDTOQuery.getIds());
            }

            if (StringUtils.isNotBlank(transportUserDTOQuery.getLogonName())) {
                query.put("logonName", transportUserDTOQuery.getLogonName());
            }

            if (StringUtils.isNotBlank(transportUserDTOQuery.getNickName())) {
                query.put("nickName", transportUserDTOQuery.getNickName());
            }

            if (StringUtils.isNotBlank(transportUserDTOQuery.getTrueName())) {
                query.put("trueName", transportUserDTOQuery.getTrueName());
            }

            if (StringUtils.isNotBlank(transportUserDTOQuery.getMobile())) {
                query.put("mobile", transportUserDTOQuery.getMobile());
            }

            if (transportUserDTOQuery.getUserType() != null && transportUserDTOQuery.getUserType() != -9999) {
                query.put("userType", transportUserDTOQuery.getUserType());
            }

            if (transportUserDTOQuery.getUserLevel() != null && transportUserDTOQuery.getUserLevel() != -9999) {
                query.put("userLevel", transportUserDTOQuery.getUserLevel());
            }

            if (transportUserDTOQuery.getCompanyIds() != null && !transportUserDTOQuery.getCompanyIds().equals("")) {
                query.put("companyIds", transportUserDTOQuery.getCompanyIds());
            }

            if (transportUserDTOQuery.getIsBindWxapp() != null) {
                query.put("isBindWxapp", transportUserDTOQuery.getIsBindWxapp());
            }

            if (transportUserDTOQuery.getLastLoginTime() != null) {
                query.put("lastLoginTime", transportUserDTOQuery.getLastLoginTime());
            }

            if (transportUserDTOQuery.getLastLoginIp() != null) {
                query.put("lastLoginIp", transportUserDTOQuery.getLastLoginIp());
            }

            if (transportUserDTOQuery.getLoginTimes() != null) {
                query.put("loginTimes", transportUserDTOQuery.getLoginTimes());
            }

            if (transportUserDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportUserDTOQuery.getStopFlag());
            }

            if (transportUserDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportUserDTOQuery.getDeleteFlag());
            }

            return transportUserService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportUser update by query failed.", e);
            throw new RuntimeException("TransportUser update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUserDTO selectById(Integer id) {
        try {
            return convert(transportUserService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportUser select by id failed.", e);
            throw new RuntimeException("TransportUser select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUserDTO selectById(Integer id, Integer entId) {
        return convert(transportUserService.selectById(id, entId));
    }

    @Override
    public TransportUserDTO selectObjByQuery(TransportUserDTOQuery transportUserDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportUserDTOQuery.getEntId() != null) {
                query.put("entId", transportUserDTOQuery.getEntId());
            }

            if (StringUtils.isNotBlank(transportUserDTOQuery.getLikeName())) {
                query.put("likeName", transportUserDTOQuery.getLikeName());
            }

            if (StringUtils.isNotBlank(transportUserDTOQuery.getLikeNickName())) {
                query.put("likeNickName", transportUserDTOQuery.getLikeNickName());
            }

            if (StringUtils.isNotBlank(transportUserDTOQuery.getLogonName())) {
                query.put("logonName", transportUserDTOQuery.getLogonName());
            }

            if (StringUtils.isNotBlank(transportUserDTOQuery.getNickName())) {
                query.put("nickName", transportUserDTOQuery.getNickName());
            }

            if (StringUtils.isNotBlank(transportUserDTOQuery.getTrueName())) {
                query.put("trueName", transportUserDTOQuery.getTrueName());
            }

            if (StringUtils.isNotBlank(transportUserDTOQuery.getMobile())) {
                query.put("mobile", transportUserDTOQuery.getMobile());
            }

            if (transportUserDTOQuery.getUserType() != null && transportUserDTOQuery.getUserType() != -9999) {
                query.put("userType", transportUserDTOQuery.getUserType());
            }

            if (transportUserDTOQuery.getUserLevel() != null && transportUserDTOQuery.getUserLevel() != -9999) {
                query.put("userLevel", transportUserDTOQuery.getUserLevel());
            }

            if (transportUserDTOQuery.getRelId() != null && transportUserDTOQuery.getRelId() > 0) {
                query.put("relId", transportUserDTOQuery.getRelId());
            }

            if (transportUserDTOQuery.getManageId() != null && transportUserDTOQuery.getManageId() > 0) {
                query.put("manageId", transportUserDTOQuery.getManageId());
            }

            if (transportUserDTOQuery.getDriverId() != null && transportUserDTOQuery.getDriverId() > 0) {
                query.put("driverId", transportUserDTOQuery.getDriverId());
            }

            if (transportUserDTOQuery.getStaffId() != null && transportUserDTOQuery.getStaffId() > 0) {
                query.put("staffId", transportUserDTOQuery.getStaffId());
            }

            if (transportUserDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportUserDTOQuery.getStopFlag());
            }

            if (transportUserDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportUserDTOQuery.getDeleteFlag());
            }

            if (transportUserDTOQuery.getCompanyIds() != null && !transportUserDTOQuery.getCompanyIds().equals("")) {
                query.put("companyIds", transportUserDTOQuery.getCompanyIds());
            }

            if (StringUtils.isNotBlank(transportUserDTOQuery.getOrderBy())) {
                query.put("orderBy", transportUserDTOQuery.getOrderBy());
            }

            return convert(transportUserService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportUser select obj by query failed.", e);
            throw new RuntimeException("TransportUser select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportUserDTO> getListForPage(TransportUserDTOQuery transportUserDTOQuery) {
        Query query = Query.page(transportUserDTOQuery.getPn(), transportUserDTOQuery.getPageSize());

        if (transportUserDTOQuery.getId() != null && transportUserDTOQuery.getId() > 0) {
            query.put("id", transportUserDTOQuery.getId());
        }

        if (transportUserDTOQuery.getIds() != null && !transportUserDTOQuery.getIds().equals("")) {
            query.put("ids", transportUserDTOQuery.getIds());
        }

        if (transportUserDTOQuery.getIotRegionId() != null && transportUserDTOQuery.getIotRegionId() > 0) {
            query.put("iotRegionId", transportUserDTOQuery.getIotRegionId());
        }

        if (transportUserDTOQuery.getManageOrDriver() != null) {
            query.put("manageOrDriver", transportUserDTOQuery.getManageOrDriver());
        }

        if (StringUtils.isNotBlank(transportUserDTOQuery.getLikeName())) {
            query.put("likeName", transportUserDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportUserDTOQuery.getLikeEnterpriseName())) {
            query.put("likeEnterpriseName", transportUserDTOQuery.getLikeEnterpriseName());
        }

        if (StringUtils.isNotBlank(transportUserDTOQuery.getMobileLike())) {
            query.put("mobileLike", transportUserDTOQuery.getMobileLike());
        }

        if (StringUtils.isNotBlank(transportUserDTOQuery.getDetailAddress())) {
            query.put("detailAddress", transportUserDTOQuery.getDetailAddress());
        }

        if (transportUserDTOQuery.getRegisterType() != null && transportUserDTOQuery.getRegisterType() == 2) {
            query.put("registerType", transportUserDTOQuery.getRegisterType());
        }

        if (transportUserDTOQuery.getNotRegisterType() != null && transportUserDTOQuery.getNotRegisterType() == 2) {
            query.put("notRegisterType", transportUserDTOQuery.getNotRegisterType());
        }

        if (StringUtils.isNotBlank(transportUserDTOQuery.getShowDate())) {
            query.put("showDate", transportUserDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportUserDTOQuery.getStartDate())) {
            query.put("startDate", transportUserDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportUserDTOQuery.getEndDate())) {
            query.put("endDate", transportUserDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportUserDTOQuery.getOrderBy())) {
            query.put("orderBy", transportUserDTOQuery.getOrderBy());
        }

        if (StringUtils.isNotBlank(transportUserDTOQuery.getLogonName())) {
            query.put("logonName", transportUserDTOQuery.getLogonName());
        }

        if (StringUtils.isNotBlank(transportUserDTOQuery.getNickName())) {
            query.put("nickName", transportUserDTOQuery.getNickName());
        }

        if (StringUtils.isNotBlank(transportUserDTOQuery.getTrueName())) {
            query.put("trueName", transportUserDTOQuery.getTrueName());
        }

        if (StringUtils.isNotBlank(transportUserDTOQuery.getMobile())) {
            query.put("mobile", transportUserDTOQuery.getMobile());
        }

        if (transportUserDTOQuery.getUserType() != null && transportUserDTOQuery.getUserType() != -9999) {
            query.put("userType", transportUserDTOQuery.getUserType());
        }

        if (transportUserDTOQuery.getUserLevel() != null && transportUserDTOQuery.getUserLevel() != -9999) {
            query.put("userLevel", transportUserDTOQuery.getUserLevel());
        }

        if (transportUserDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportUserDTOQuery.getStopFlag());
        }

        if (transportUserDTOQuery.getCompanyIds() != null && !transportUserDTOQuery.getCompanyIds().equals("")) {
            query.put("companyIds", transportUserDTOQuery.getCompanyIds());
        }

        List<TransportUserDTO> list = convert(transportUserService.selectByQuery(query));
        Page<TransportUserDTO> page = Page.from(transportUserDTOQuery.getPn(), transportUserDTOQuery.getPageSize(), list);
        page.setTotalItems(transportUserService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportUserService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportUser delete failed.", e);
            throw new RuntimeException("TransportUser delete failed, Please order your code.", e);
        }
    }

    @Override
    public List<Map<String, String>> selectSimilarUser(String similar, Integer userType) {
        try {
            Query query = Query.page(1, 20);

            if (StringUtils.isNotBlank(similar)) {
                query.put("likeName", similar.trim());
            }

            if (userType != null && userType == 10) {
                query.put("existManageId", userType);
            }

            if (userType != null && userType == 20) {
                query.put("existDriverId", userType);
            }

            if (userType != null && userType == 30) {
                query.put("existStaffId", userType);
            }

            return transportUserService.selectSimilarUser(query);

        } catch (Exception e) {
            logger.error("TransportUser select similar user failed.", e);
            throw new RuntimeException("TransportUser similar user failed, Please order your code.", e);
        }
    }

    @Override
    public List<Map<String, String>> getMapDataByQuery(TransportUserDTOQuery transportUserDTOQuery, String element) {
        try {
            Query query = Query.noPage();
            query.put("stopFlag", 0);
            query.put("deleteFlag", 0);

            return transportUserService.getMapDataByQuery(query, element);
        } catch (Exception e) {
            logger.error("TransportUser select map data failed.", e);
            throw new RuntimeException("TransportUser select map data failed, Please order your code.", e);
        }
    }
}